﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ITOMMS.Entity;
using ITOMMS.Business;
using Watson.Core.Tools;

namespace ITOMMS.Web.Notice
{
    public partial class NoticeEdit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                    }
                }
            }
        }

        private void DataShow(int id)
        {
            tbNoticeEntity entity = new tbNoticeEntity();
            entity = entity.GetById(id);
            ViewState["Entity"] = entity;
            if (entity != null)
            {
                this.txtTitle.Text = entity.Title;
                this.FreeTextBox1.Text = entity.Content;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tbNoticeEntity entity = new ITOMMS.Entity.tbNoticeEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbNoticeEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Now;
                }

                entity.Title = txtTitle.Text;
                entity.Content = FreeTextBox1.Text;
                entity.SaveOrUpdate();
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("NoticeList.aspx",true);
        }
    }
}
